(function() {
	function D(s) {
		document.write(s + "<br/>");
	}
	function getQueryString(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
		var r = window.location.search.substr(1).match(reg);
		if (r != null) {
			val = unescape(r[2]);
			if (val != null && val.length > 0) {
				return val;
			}
		}
		return null;
	}
	function setCookie(name, value, hours) {
		var str = name + "=" + unescape(value);
		if (hours > 0) {
			var date = new Date();
			var ms = hours * 3600 * 1000;
			date.setTime(date.getTime() + ms);
			str += "; expires=" + date.toGMTString();
		}
		str += "; domain=.lenovomobile.com; path=/";
		document.cookie = str;
	}
	function getCookie(name) {
		var cookiestring = document.cookie;
		var start = cookiestring.indexOf(name + '=');
		if (start == -1)
			return null;
		start += name.length + 1;
		var end = cookiestring.indexOf("; ", start);
		if (end == -1)
			return unescape(cookiestring.substring(start));
		return unescape(cookiestring.substring(start, end));
	}

	function CKSource() {
		this.sourceid_paramters_name = "cid";
		this.referrer_paramters_name = "ref";
		this.sourceid = null;
		this.default_sourceid = "ZUK";
		this.sourceid_name = "source_cid";
		this.sourceid_value = null;
		this.sourceid_time = 360;
		/*shopuuid*/
		this.shopuuid_name = "shopuuid";
		this.shopuuid = null;
		this.shopuuid_value = null;
		this.shopuuid_time = 360;
		/*feedback*/
		this.feedback_paramters_name = "feedback";
		this.feedback_name = "feedback";
		this.feedback = null;
		this.feedback_value = null;
		this.feedback_time = 360;
		/*pid*/
		this.pid_paramters_name = "pid";
		this.pid_name = "pid";
		this.pid = null;
		this.pid_value = null;
		this.pid_time = 0;
		/* first */
		this.first_sourceid_time = 87600;
		this.first_sourceid_name = "first_sourceid";
		this.first_sourceid_value = null;
		this.first_domain_time = 87600;
		this.first_domain_name = "first_domain";
		this.first_domain_value = null;

		/* last */
		this.last_sourceid_time = 0;
		this.last_sourceid_name = "last_sourceid";
		this.last_sourceid_value = null;
		this.last_domain_time = 0;
		this.last_domain_name = "last_domain";
		this.last_domain_value = null;

		this.domains=[{"def":false,"myself":true,"domain":".lenovomobile.com","cid":this.default_sourceid},
		              {"def":true,"domain":"OTHER","cid":"other"}];
	}
	CKSource.prototype.initValues = function() {
		this.sourceid = getQueryString(this.sourceid_paramters_name);
		this.sourceid_value = getCookie(this.sourceid_name);
		this.feedback = getQueryString(this.feedback_paramters_name);
		this.feedback_value = getCookie(this.feedback_name);
		this.pid = getQueryString(this.pid_paramters_name);
		this.pid_value = getCookie(this.pid_name);
		this.first_sourceid_value = getCookie(this.first_sourceid_name);
		this.first_domain_value = getCookie(this.first_domain_name);
		this.last_sourceid_value = getCookie(this.last_sourceid_name);
		this.last_domain_value = getCookie(this.last_domain_name);

	}
	CKSource.prototype.debug = function() {
		
		console.log("first_cid:" + getCookie(this.first_sourceid_name), "first_domain:" + getCookie(this.first_domain_name), "last_cid:" + getCookie(this.last_sourceid_name), "last_domain:" + getCookie(this.last_domain_name), "source_cid:" + getCookie(this.sourceid_name), "feedback:" + getCookie(this.feedback_name));
	}
	CKSource.prototype.d = function() {
		
		D("first_cid:" + getCookie(this.first_sourceid_name) + "@@first_domain:" + getCookie(this.first_domain_name)+ "last_cid:" + getCookie(this.last_sourceid_name)+ "@@last_domain:" + getCookie(this.last_domain_name) + "@@source_cid:" + getCookie(this.sourceid_name) + "@@feedback:" + getCookie(this.feedback_name));
	}
	CKSource.prototype.initReferrer = function() {
		if (document.referrer != null && document.referrer.length > 0) {
			this.referrer = document.referrer;
		}
		if(!this.referrer)
		{
			this.referrer = null;
		}
	}
	CKSource.prototype.isMyselfReferrer = function(referrer,refDomain){
		if(referrer && referrer.toLowerCase().indexOf("http")==0)
		{
			var urlArr = referrer.split('/');
			if (urlArr && urlArr.length > 2) {
			   return urlArr[2].indexOf(refDomain.domain) != -1;
			}
		}
		else if(referrer && referrer.lastIndexOf(refDomain.domain) != -1)
		{
			return true;
		}
		return false;
	}
	CKSource.prototype.getReferrerDomain = function(referrer) {
		for ( var i in this.domains) {
			if (this.domains[i].def) {
				var d = this.domains[i];
				var urlArr = referrer.split('/');
				if (urlArr && urlArr.length > 2) {
					d.domain = urlArr[2];
				}
				return d;
			} else {
				var myself =this.isMyselfReferrer(referrer,this.domains[i]); 
				if (myself) {
					var d = this.domains[i];
					var urlArr = referrer.split('/');
					if (urlArr && urlArr.length > 2) {
						d.domain = urlArr[2];
					}
					return d;
				}
			}
		}
	}
	CKSource.prototype.getUAPlatformtype = function() {
		var ua = window.navigator.userAgent.toLowerCase();
		var weixin = new RegExp(/MicroMessenger/ig);
		var weibo = new RegExp(/Weibo/ig);
		var qq = new RegExp(/\sQQ\//ig);
		if (weixin.test(ua)) {
			return "UA_weixin";
		}
		if (weibo.test(ua)) {
			return "UA_weibo";
		}
		if (qq.test(ua)) {
			return "UA_qq";
		}
		return false;
	}

	CKSource.prototype.initFirstCKSourceId = function() {
		if (this.first_sourceid_value == null) {
			if (this.sourceid != null) {
				setCookie(this.first_sourceid_name, this.sourceid, this.first_sourceid_time);
			} else {
				var cl = this.getUAPlatformtype();
				if (this.referrer == null) {
					if (cl) {
						setCookie(this.first_sourceid_name, cl, this.first_sourceid_time);
					} else {
						setCookie(this.first_sourceid_name, this.default_sourceid, this.first_sourceid_time);
					}
				} else {
					var ref_domain = this.getReferrerDomain(this.referrer);
					if (ref_domain.myself) {
						if(cl){
							setCookie(this.first_sourceid_name, cl, this.first_sourceid_time);
						}else{
							setCookie(this.first_sourceid_name,this.default_sourceid, this.first_sourceid_time);
						}
					} else {
						setCookie(this.first_sourceid_name, ref_domain.domain, this.first_sourceid_time);
					}
				}
			}
		}
	}
	CKSource.prototype.initFirstCKSourceDomain = function() {
		if (this.first_domain_value == null) {
			var ref = this.referrer == null ? window.location.href : this.referrer;
			var ref_domain = this.getReferrerDomain(ref);
			setCookie(this.first_domain_name, ref_domain.domain, this.first_domain_time);
		}
	}

	CKSource.prototype.initLastCKSourceId = function() {
		if (this.sourceid != null) {
			setCookie(this.last_sourceid_name, this.sourceid, this.last_sourceid_time);
		} else {
			var cl = this.getUAPlatformtype();
			if (this.referrer == null) {
				if (this.last_sourceid_value == null) {
					if (cl) {
						setCookie(this.last_sourceid_name, cl, this.last_sourceid_time);
					} else {
						setCookie(this.last_sourceid_name, this.default_sourceid, this.last_sourceid_time);
					}
				}
			} else {
				var ref_domain = this.getReferrerDomain(this.referrer);
				if (ref_domain.myself) {
					this.initValues();
					if (this.last_sourceid_value == null) {
						if (cl) {
							setCookie(this.last_sourceid_name, cl, this.last_sourceid_time);
						} else {
							setCookie(this.last_sourceid_name, this.default_sourceid, this.last_sourceid_time);
						}
					}
				} else {
					setCookie(this.last_sourceid_name, ref_domain.domain, this.last_sourceid_time);
				}
			}
		}

	}
	CKSource.prototype.initLastCKSourceDomain = function() {
		if (this.last_domain_value == null) {
			var ref = this.referrer == null ? window.location.href : this.referrer;
			var ref_domain = this.getReferrerDomain(ref);
			setCookie(this.last_domain_name, ref_domain.domain, this.last_domain_time);
		} else {
			if (this.referrer != null) {
				var ref_domain = this.getReferrerDomain(this.referrer);

				if (!ref_domain.myself) {
					setCookie(this.last_domain_name, ref_domain.domain, this.last_domain_time);
				} else {
					this.initValues();
					if (this.last_domain_value == null) {
						setCookie(this.last_domain_name, ref_domain.domain, this.last_domain_time);
					}
				}
			}
		}
	}
	CKSource.prototype.initCKSourceId = function() {
		if (this.sourceid_value == null) {
			var cid = getQueryString("cid");
			var feedback = getQueryString("feedback");
			var pid = getQueryString("pid");
			if (cid == null) {
				var ref = this.referrer == null ? window.location.href : this.referrer;
				var ref_domain = this.getReferrerDomain(ref);
				
				if (ref_domain.myself) {
					var cl = this.getUAPlatformtype();
					
					if (cl) {
						setCookie(this.sourceid_name, cl, this.sourceid_time);
					} else {
						setCookie(this.sourceid_name, this.default_sourceid, this.sourceid_time);
					}
				} else {
					setCookie(this.sourceid_name, ref_domain.domain, this.sourceid_time);
				}
			} else {
				setCookie(this.sourceid_name, cid, this.sourceid_time);
				if(feedback != null && feedback != ""){
					setCookie(this.feedback_name, feedback, this.feedback_time);
				}
				if(pid != null && pid != ""){
					setCookie(this.pid_name, pid, this.pid_time);
				}
			}
		} else{
			var feedback = getQueryString("feedback") || "";
			if(feedback != null && feedback != ""){
				setCookie(this.feedback_name, feedback, this.feedback_time);
			}
			var pid = getQueryString("pid") || "";
			if(pid != null && pid != ""){
				setCookie(this.pid_name, pid, this.pid_time);
			}
		}
		var shopuuid = getCookie(this.shopuuid_name) || "";
		if(shopuuid == null || shopuuid == "" || shopuuid == 'null'){
			shopuuid = createUUID();
		}
		setCookie(this.shopuuid_name, shopuuid, this.shopuuid_time);
	}
	CKSource.prototype.initThreeSidesLnv = function() {
			var ln_v = getQueryString("ln_v");
			if ("" != ln_v && ln_v != null && ln_v != 'undefined') {
				setCookie("ln_v", ln_v, 720);
			}
	}
	var ck = new CKSource();
	ck.initValues();
	ck.initReferrer();
	ck.initCKSourceId();
	ck.initFirstCKSourceId();
	ck.initFirstCKSourceDomain();
	ck.initLastCKSourceId();
	ck.initLastCKSourceDomain();
	ck.initThreeSidesLnv();
	/*ck.d();*/
	
	function createUUID(){
	    var dg = new Date(1582, 10, 15, 0, 0, 0, 0);
	    var dc = new Date();
	    var t = dc.getTime() - dg.getTime();
	    var tl = getIntegerBits(t,0,31);
	    var tm = getIntegerBits(t,32,47);
	    var thv = getIntegerBits(t,48,59) + '1'; // version 1, security version is 2
	    var csar = getIntegerBits(rand(4095),0,7);
	    var csl = getIntegerBits(rand(4095),0,7);
	    var n = getIntegerBits(rand(8191),0,7) +
	            getIntegerBits(rand(8191),8,15) +
	            getIntegerBits(rand(8191),0,7) +
	            getIntegerBits(rand(8191),8,15) +
	            getIntegerBits(rand(8191),0,15); // this last number is two octets long
	    return tl + tm  + thv  + csar + csl + n;
	};
	function getIntegerBits(val,start,end){
	 var base16 = returnBase(val,16);
	 var quadArray = new Array();
	 var quadString = '';
	 var i = 0;
	 for(i=0;i<base16.length;i++){
	     quadArray.push(base16.substring(i,i+1));   
	 }
	 for(i=Math.floor(start/4);i<=Math.floor(end/4);i++){
	     if(!quadArray[i] || quadArray[i] == '') quadString += '0';
	     else quadString += quadArray[i];
	 }
	 return quadString;
	};

	function returnBase(number, base){
	 return (number).toString(base).toUpperCase();
	};

	function rand(max){
	 return Math.floor(Math.random() * (max + 1));
	};
})();